

#include <avr/wdt.h>
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


#define pin_r_1      4
#define pin_r_2      5

#define pin_a_1      2
#define pin_a_2      5

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 3);

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

#define SYSLOG_DEFAULT_PORT 514

EthernetUDP Udp;


/*Canal 1 Incendie
 #Canal 2 Panique
 #Canal 3 Intrusion
 #Canal 4 Mise en Service/Mise hors Service
 #Canal 5 Annuler
 #Canal 6 Mis en Service Partielle
 #Canal 7 Code “Sous Contrainte”
 #Canal 8 Deuxième Intrusion
 */

void setup(){
  wdt_enable(WDTO_8S);
  Serial.begin(115200);
  pinMode(pin_r_1, OUTPUT);
  pinMode(pin_r_2, OUTPUT);
  digitalWrite(pin_r_1, LOW);
  digitalWrite(pin_r_2, LOW);

  Ethernet.begin(mac,ip);
  Udp.begin(SYSLOG_DEFAULT_PORT);


  Serial.println(F("Beginning ... ")); 
}

void loop(){
  wdt_reset();
  static long last = 0;
  if (millis() - last >= 120000)
  {
    //Serial.println(F("no new from 120s"));
    digitalWrite(pin_r_1, LOW);
    digitalWrite(pin_r_2, LOW);
    last = millis();   //pour ne pas faire trop d'affichage !!
    delay(20000); // for reboot
  }
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    char relay1 = packetBuffer[0];
    char relay2 = packetBuffer[2];
    digitalWrite(pin_r_1, (relay1 == 0) ? HIGH:LOW);
    digitalWrite(pin_r_2, (relay2 == 0) ? HIGH:LOW);
    Serial.print(relay1);
    Serial.print(":");
    Serial.print(relay2);

  }
}








